import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/education/custom_new_title_chart_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/demand_research/education_survey_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/education/me_demand_research/all_ing_research__controller.dart';

///需求调研（进行中）
class AllIngResearchPage extends StatelessWidget {
  AllIngResearchPage({super.key});

  final controller = Get.put(AllIngResearchController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            backgroundColor: AppColors.bg_color_grey,
            appBar: CustomAppBar(
              titleText: '需求调研',
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<EducationSurveyListEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.educationSurveyList,
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  )),
                ],
              ),
            ),
            // 右侧筛选栏
            endDrawer: CustomEndDrawer(
              centerContext: Column(
                children: [
                  _selectType('调研类型', controller.workKindNameBack ?? ''),
                  _alignTitle('培训时间'),
                  AppGaps.vGap16,
                  CustomSelectTimePeriod(
                    startTime: controller.startDate,
                    endTime: controller.endDate,
                    onConfirm: (start, end) {
                      controller.startDate = start;
                      controller.endDate = end;
                    },
                    showBorder: true,
                    iconCalendar: AppImages.icon__screen_clock,
                  ),
                ],
              ),
              reset: controller.reset,
              confirm: controller.confirm,
            ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    controller: TextEditingController(text: controller.searchX),
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入调研名称搜索',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _rowData(String name, String value, {Alignment alignment = Alignment.centerLeft}) {
    return Row(
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 13.sp,
          ),
        ),
        Expanded(
          child: Align(
            alignment: alignment,
            child: Text(
              value,
              style: TextStyle(
                color: const Color(0xFF495061),
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
        )
      ],
    );
  }

  ///	状态 0：待提交 1：进行中 2：已结束
  String _strSurveyStatus(int? surveyStatus) {
    switch (surveyStatus) {
      case 0:
        return '待提交';
      case 1:
        return '进行中';
      case 2:
        return '已结束';
      default:
        return '';
    }
  }

  ///	状态 0：待提交 1：进行中 2：已结束
  Color _strSurveyStatusColors(int? surveyStatusColors) {
    switch (surveyStatusColors) {
      case 0:
        return const Color(0xFFFC9900);
      case 1:
        return const Color(0xFF00CE78);
      case 2:
        return const Color(0xFFA5A9C8);
      default:
        return const Color(0xFFA5A9C8);
    }
  }

  Widget _itemWidget(EducationSurveyListEntity item) {
    return CustomNewTitleChartPage(
      title: '${item.surveyName}',
      titleRightWidget: Container(
        width: 52.w,
        height: 18.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: _strSurveyStatusColors(item.surveyStatus),
            borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(10),
              topRight: Radius.circular(10),
              bottomRight: Radius.circular(10),
            )),
        child: Text(
          _strSurveyStatus(item.surveyStatus),
          style: TextStyle(
            color: Colors.white,
            fontSize: 10.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _rowData('调研类型：', item.surveyType == 0 ? '调查问卷' : '调研测验', alignment: Alignment.centerLeft),
          AppGaps.vGap12,
          Row(
            children: [
              Expanded(
                child: _rowData('开始时间：', item.surveyStartDate ?? '', alignment: Alignment.centerLeft),
              ),
              Container(
                padding: EdgeInsets.only(left: 14.w, right: 14.w, top: 2.w, bottom: 2.w),
                decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.all(Radius.circular(8.r)),
                ),
                child: Text(
                  '进入',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.w,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ).clickOnce(() {
                controller.inputClickPage(item);
              })
            ],
          )
        ],
      ),
    ).click(() {
      controller.onClickButtonPressed(item);
    });
  }


  Widget _selectType(String firstName, String name) {
    return Column(
      children: [
        _alignTitle(firstName),
        AppGaps.vGap16,
        Container(
          height: 38.w,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            border: Border.all(width: 1.w, color: const Color(0xFFE3E7EE)),
          ),
          child: Row(
            children: [
              AppGaps.hGap20,
              Expanded(
                child: Text(
                  name,
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: const Color(0xFF495061),
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
              SvgPicture.asset(AppImages.icon_filter_right_arrow),
              AppGaps.hGap20,
            ],
          ),
        ).clickOnce(() {
          if (firstName == '调研类型') {
            controller.selectHiddenDangerInvestigationTaskWorkKindType();
          }
        }),
        AppGaps.vGap16,
      ],
    );
  }

  Widget _alignTitle(String title) {
    return Align(
      alignment: Alignment.centerLeft,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w500,
        ),
      ),
    );
  }
}
